const express = require('express');

const common = require('../../libs/common');
const mysql = require('mysql');

const pathLib = require('path');
const fs = require('fs');

var db = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'mysql123456',
    database: '180618'
});

module.exports = function(){
    var router = express.Router();


    router.get('/',(req, res)=>{
        //get请求，修改、删除数据
        switch(req.query.act){
            case 'mod':
                console.log('mod');
                    db.query(`select * from custom_evaluation_table WHERE id=${req.query.id}`,(err, data)=>{
                        if(err){
                            console.log(err);
                            res.status(500).send('database error').end();
                        }else{
                            db.query(`select * from custom_evaluation_table`,(err, evaluations)=>{
                                if(err){
                                    res.status(500).send('database error').end();
                                }else{
                                    console.log(data[0]);
                                    res.render('admin/custom.ejs',{evaluations,mod_data:data[0]});
                                }
                            });  
                        }
                    });
                break;
            case 'del':
                console.log('del');
                db.query(`select * from custom_evaluation_table where ID=${req.query.id}`,(err, data)=>{
                    if(err){
                        console.log(err);
                        res.status(500).send('database error').end();
                    }else{
                        if(data.length==0){
                            res.status(404).send('no this custom evaluation').end();
                        }else{
                            console.log(data[0]);
                            fs.unlink('static/upload/'+data[0].src,(err)=>{
                                if(err){
                                    console.log(err);
                                    res.status(500).send('file opration error').end();
                                }else{
                                    db.query(`delete from custom_evaluation_table where ID=${req.query.id}`,(err, data)=>{
                                        if(err){
                                            console.log(err);
                                            res.status(500).send('database error').end();
                                        }else{
                                            console.log('重定向到custom');
                                            res.redirect('/admin/custom');
                                        }
                                    });
                                }
                            });
                        }
                    }

                }); 
                break;
            default:
                db.query(`select * from custom_evaluation_table`,(err,evaluations)=>{
                    if(err){
                        console.log(err);
                        res.status(500).send('database error').end();
                    }else{
                        res.render('admin/custom.ejs',{evaluations})
                    }
                });
                break;
        }
    });

    //custom请求
    router.post('/',(req, res)=>{
        //console.log('custom请求 post 请求'+req.files);

        var title = req.body.title;
        var description = req.body.description;
        //var href = req.body.href;

        if(req.files[0]){
            //修改图片名称,自动保存到server设置的upload中
            var ext = pathLib.parse(req.files[0].originalname).ext;
            var oldPath = req.files[0].path;
            var newPath = req.files[0].path + ext;
            var newFileName = req.files[0].filename + ext;

            fs.rename(oldPath,newPath,(err)=>{
                if(err){
                    res.status(500).send('file opration error ').end();
                }else{
                    console.log('图片上传成功');
                }
            });
        }else{
            newFileName = null;
        } 

        if(!title || !description){
            res.status(400).send('arg error').end();
        }else{
            if(req.body.mod_id){
                //修改，1.没有上传图片但是有图片,就用原来的  2.没有上传图片、也没有图片
                console.log('xxxxxx:'+req.body.mod_id);

                if(newFileName == null){
                    db.query(`select * from custom_evaluation_table where id = ${req.body.mod_id}`,(err,data)=>{
                        if(err){
                            
                        }else{
                            newFileName = data[0].src;
                            db.query(`update custom_evaluation_table set title='${req.body.title}',
                            description='${req.body.description}',
                            src='${newFileName}' where id=${req.body.mod_id}`,(err, data)=>{
                                if(err){
                                    res.status(500).send('database error').end;
                                }else{
                                    res.redirect('/admin/custom');
                                }
                            });
                        }
                    });
                }else{
                    //优化,把老的删掉，再更新新的图片
                    db.query(`update custom_evaluation_table set title='${req.body.title}',
                    description='${req.body.description}',
                    src='${newFileName}' where id=${req.body.mod_id}`,(err, data)=>{
                        if(err){
                            res.status(500).send('database error').end;
                        }else{
                            res.redirect('/admin/custom');
                        }
                    });
                }
            }else{
                db.query(`insert into custom_evaluation_table (title, description, src)
                value('${title}','${description}','${newFileName}')`,(err, data)=>{
                    if(err){
                        res.status(500).send('database error').end;
                    }else{
                        res.redirect('/admin/custom');
                    }
                });
            }
        }
    });

    return router;
}